package net.wenku8.reader.view.online

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.LocalNavController
import net.wenku8.reader.cache.CoverCacheHandler
import net.wenku8.reader.enums.RouteOnlineDetail
import net.wenku8.reader.model.book.BookPreviewInfo
import net.wenku8.reader.state.StateReadOnline
import net.wenku8.reader.ui.lite.LiteCard
import net.wenku8.reader.ui.lite.LiteText
import net.wenku8.reader.ui.lite.LiteTitle
import net.wenku8.reader.ui.lite.cardImage
import net.wenku8.reader.ui.theme.LiteRed
import net.wenku8.reader.view.common.CacheImage
import net.wenku8.reader.view.common.DialogLayer
import net.wenku8.reader.view.common.DialogViewModel

@Composable
fun OnlineBookPreview(bookPreviewInfo: BookPreviewInfo, bookList : Boolean = true){
    val navController = LocalNavController.current
    val dialogViewModel = viewModel<DialogViewModel>()
    LiteCard(
        left = {
            val cacheHandler = remember { CoverCacheHandler(bookPreviewInfo.bookId) }
            CacheImage(cacheHandler, modifier = Modifier.cardImage(), ContentScale.Crop)
        },
        header = {
            LiteTitle(bookPreviewInfo.title, color = LiteRed)
        },
        onClick = {
            if (bookList){
                StateReadOnline.bookId.value = bookPreviewInfo.bookId
                navController.navigate(RouteOnlineDetail)
            } else {
                dialogViewModel.show(bookPreviewInfo.title)
            }
        }
    ){
        Column{
            if (bookPreviewInfo.inBookcase){
                LiteText(text = "更新时间：" + bookPreviewInfo.lastUpdate)
                LiteText(text = "最新章节：" + bookPreviewInfo.lastChapter)
            }else{
                LiteText(text = "小说作者：" + bookPreviewInfo.author)
                LiteText(text = "更新时间：" + bookPreviewInfo.lastUpdate)
            }
        }
    }
    DialogLayer()
}

